package com.kungfuhacking.wristbandpro.location.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.base.view.BaseActivity;
import com.kungfuhacking.wristbandpro.custom.TitleBarView;
import com.kungfuhacking.wristbandpro.location.bean.AreaListEntity;
import com.kungfuhacking.wristbandpro.location.presenter.ISafeDetailPresenter;
import com.kungfuhacking.wristbandpro.location.view.ISafeDetailView;


/**
 * 安全区域详情页面
 * Created by Aaron on 17/5/8.
 */

public class SafeDetailActivity extends BaseActivity implements ISafeDetailView {

    private AreaListEntity areaEntity;
    private TitleBarView tbv;
    private MapView mapView;
    private ISafeDetailPresenter iSafeDetailPresenter = new ISafeDetailPresenter(this);
    private BitmapDescriptor des = null;
    private AMapLocationClient mLocationClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_safe_detail);
        initWidget();
        mapView.onCreate(savedInstanceState);// 此方法必须重写

        if (bundle != null)
            areaEntity = bundle.getParcelable("areaDetail");

        initLocation();

    }

    private void initLocation() {
        mLocationClient = new AMapLocationClient(this);
        iSafeDetailPresenter.initMap(mapView.getMap(), mLocationClient);

        if (des == null) {
            Bitmap bMap = BitmapFactory.decodeResource(this.getResources(),
                    R.mipmap.ic_thumb);
            des = BitmapDescriptorFactory.fromBitmap(bMap);
        }

        if (areaEntity != null)
            iSafeDetailPresenter.addLocationMarker(areaEntity, des);

    }

    private void initWidget() {
        this.mapView = (MapView) findViewById(R.id.gdmap);
        this.tbv = (TitleBarView) findViewById(R.id.tbv);

        tbv.setOnLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        // 在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        if (mapView != null)
            mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mapView != null)
            mapView.onSaveInstanceState(outState);
    }

    @Override
    public void onPause() {
        super.onPause();
        // 在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        if (mapView != null)
            mapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        iSafeDetailPresenter.clearRequest();
        if (mapView != null)
            mapView.onDestroy();
    }
}
